<?php
/**
* Build an HTML page from <<html>> to <</hhtml>> tags.
*The constructor will make sure that all the required javascrip files and css are included.
*The main entry point is the <i>GeneratePage</i> method.
*@package Html
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@todo Filter JS based on reserved in filename.
*/
class Html extends BaseClassSingleton
{
	private $head_scripts = array();

	/**
	*Javascript code is cached and minified when in deployment mode, otherwise is included as is in debug.
	*/
	public function __construct()
	{
		parent::__construct();
		$profile_js = new CoreProfile("Build Javascript cache");
		# Build the Javascript area... 
		if  (Core::GetConfig("error_debug"))
		{	# When debugging, just include all the jsvascripts in the js folder.
			$files = CoreUtils::ListFolder("Engine".DIRECTORY_SEPARATOR."ClientApp", array("mask" => "/.*?js$/"));
			sort( $files );
			foreach ( $files as $file )
				$this->head_scripts[] = "Engine/ClientApp/$file";
		}
		else
		{	# When in production, use the cached JS minified library...
			if ( !file_exists( "Engine".DIRECTORY_SEPARATOR."ClientApp.js") || 
					(filemtime("Engine".DIRECTORY_SEPARATOR."ClientApp.js") < filemtime("Engine".DIRECTORY_SEPARATOR."ClientApp")) )
			{	# Or recreate it if it does not exists...
				$files = CoreUtils::ListFolder("Engine".DIRECTORY_SEPARATOR."ClientApp", array("mask" => "/.*?js$/"));
				sort( $files );
				$big_js = "";
				foreach ( $files as $file )
					$big_js .= file_get_contents( "Engine".DIRECTORY_SEPARATOR."ClientApp".DIRECTORY_SEPARATOR."$file" )."\n";
				$pre_size = strlen( $big_js );
				require_once BASEPATH."Libs".DIRECTORY_SEPARATOR."jsmin.php";
				$small_js = JSMin::minify( $big_js );
				$post_size = strlen( $small_js );
				file_put_contents( "Engine".DIRECTORY_SEPARATOR."ClientApp.js", $small_js );
				echo "Minified Javascript. Size before: $pre_size, size after: $post_size (ratio: ".($post_size/$pre_size*100)."%)";
			}
			$this->head_scripts[] = "Engine/ClientApp.js";
		}
		$profile_js->Stop();
	}

	/**
	*Build the page.
	*@param HtmlTag The content for the main body div
	*@return HtmlTag the entire HTML tree
	*@todo Study if possible to Profile the shutdown process
	*/
	final public function GeneratePage( $content )
	{	# We need to convert everything to string before ClientSideStartup, or the JS will not be included.
#		$profile_data = new CoreProfile("Template tree data");
#		$content = $content->PrepareData();
#		$profile_data->Stop();

	# Save path to be used for requests 
		$jquery_init = "Core.EnginePath = '".Core::GetUri("path")."';";
		# Add the JS code registered with Core::AddJsCode()
#		$jquery_init .= implode(";", Core::GetJsCode() ).";";
		# Activate CorekeepAlive request to keep users logged in
		if (CoreAuth::Authorize())
			$jquery_init .= "Core.keepAlive();";
		# hide the wait body and show the real body now that we are fully loaded...
#		$jquery_init .= "})";


		if ( CoreAuth::Authorize() )
		{
			$mgm_tabs = array(
						array("type" => "query",
							"data" => "MgmBoxes",
							"caption" => I18N("Atoms...")),
						array("type" => "query",
							"data" => "MgmStorage",
							"caption" => I18N("Storage...")),
						array("type" => "query",
							"data" => "MgmUsers",
							"caption" => I18N("Users..."))
						);
			$login_caption = I18N("Logout");
			$login_template = "core:logout";
		}
		else
		{
			$mgm_tabs = array();
			$login_caption = I18N("Login");
			$login_template = "core:login";
		}

		$profile_template = new CoreProfile("Template output");
		$open = new HtmlTemplate("core:html",
							array(
								"lang" => Core::GetConfig("lang"),
								"title" => Core::GetTitle(),
								"description" => Core::GetConfig("description"),
								"keywords" => Core::GetConfig("keywords"),
								"stylesheet" => "Theme/default/style.css.php",
								"head_scripts" => $this->head_scripts,
								"management_tabs" => $mgm_tabs,
								"login_caption" => $login_caption,
								"login_template" => $login_template,
								"content" => $content->GetId(),
								"jquery_init" => $jquery_init
								));

		$open->DrawHtml();
		$profile_template->Stop();
		CoreProfile::Dump();
	}
}
?>
